/**
 * AjaxUtils.java
 * Created at 2013-4-14
 * Created by jackie
 * Copyright (C) 2013 Jackie, All rights reserved.
 */
package com.huabo.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.apache.log4j.Logger;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>ClassName: AjaxUtils</p>
 * <p>Description: ajax工具类</p>
 * <p>Author: jackie</p>
 * <p>Date: 2013-4-14</p>
 */
public class AjaxUtils {
    /**
     * <p>
     * Field STATUS: 状态
     * </p>
     */
    public static final String CODE = "code";
    /**
     * <p>
     * Field WARN: 警告
     * </p>
     */
    public static final String DATA = "data";
    /**
     * <p>
     * Field MESSAGE: 消息
     * </p>
     */
    public static final String MESSAGE = "message";

    private static final String SUCCESS = "success";

    /**
     * <p>Field logger: 日志对象</p>
     */
    protected static Logger logger = Logger.getLogger(AjaxUtils.class);
    /**
     * <p>
     * Description: AJAX输出，返回null
     * </p>
     * 
     * @param response HttpServletResponse对象
     * @param content 内容
     * @param type contentType
     * @return null
     */
    public static String ajax(HttpServletResponse response, String content, String type) {
        try {
            response.setContentType(type + ";charset=UTF-8");
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
            response.getWriter().write(content);
            response.getWriter().flush();
        } catch (IOException e) {
            logger.error("AJAX响应错误", e);
        }
        return null;
    }

    /**
     * <p>Description: AJAX输出文本，返回null</p>
     * @param response HttpServletResponse对象
     * @param text 文本内容
     * @return null
     */
    public static String ajaxText(HttpServletResponse response, String text) {
        return ajax(response, text, "text/plain");
    }

    /**
     * <p>Description: AJAX输出HTML，返回null</p>
     * @param response HttpServletResponse对象
     * @param html html内容
     * @return null
     */
    public static String ajaxHtml(HttpServletResponse response, String html) {
        return ajax(response, html, "text/html");
    }

    /**
     * <p>Description: AJAX输出XML，返回null</p>
     * @param response HttpServletResponse对象
     * @param xml xml内容
     * @return null
     */
    public static String ajaxXml(HttpServletResponse response, String xml) {
        return ajax(response, xml, "text/xml");
    }

    /**
     * <p>Description: 根据字符串输出JSON，返回null</p>
     * @param response HttpServletResponse对象
     * @param jsonString json串
     * @return null
     */
    public static String ajaxJson(HttpServletResponse response, String jsonString) {
        return ajax(response, jsonString, "text/html");
    }

    /**
     * <p>Description: 根据Map输出JSON，返回null</p>
     * @param response HttpServletResponse对象
     * @param jsonMap json map对象
     * @return null
     */
    public static String ajaxJson(HttpServletResponse response, Map<String, String> jsonMap) {
        String result = getJsonString(jsonMap);
        return ajax(response, result, "text/html");
    }

    private static String getJsonString(Object obj) {
        return JSON.toJSONString(obj, SerializerFeature.WriteMapNullValue);
    }

    /**
     * <p>Description: 输出JSON警告消息，返回null</p>
     * @param response HttpServletResponse对象
     * @param message 信息字符串
     * @return null
     */
    public static String ajaxJsonWarnMessage(HttpServletResponse response, String message) {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put(CODE, CommonReturnCode.WARN.code());
        jsonMap.put(MESSAGE, message);
        jsonMap.put(SUCCESS, false);
        return ajax(response, getJsonString(jsonMap), "text/html");
    }

    /**
     * <p>Description: 输出JSON成功消息，返回null</p>
     * @param response HttpServletResponse对象
     * @param message 信息字符串
     * @return null
     */
    public static String ajaxJsonSuccessMessage(HttpServletResponse response, String message) {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put(CODE, CommonReturnCode.SUCCESS.code());
        jsonMap.put(MESSAGE, message);
        jsonMap.put(SUCCESS, true);
        return ajax(response, getJsonString(jsonMap), "text/html");
    }

    /**
     * <p>Description: 输出JSON成功消息，返回null</p>
     * @param response HttpServletResponse对象
     * @param data 信息字符串
     * @return null
     */
    public static String ajaxJsonSuccessMessage(HttpServletResponse response, Object data) {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put(CODE, CommonReturnCode.SUCCESS.code());
        jsonMap.put(MESSAGE, CommonReturnCode.SUCCESS.name());
        jsonMap.put(SUCCESS, true);
        jsonMap.put(DATA, data);
        return ajax(response, getJsonString(jsonMap), "text/html");
    }

    /**
     * <p>Description: 输出JSON成功消息，返回null</p>
     * @param response HttpServletResponse对象
     * @param page 分页信息
     * @return null
     */
    public static <T> String ajaxJsonSuccessPage(HttpServletResponse response, Page<T> page) {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put(CODE, CommonReturnCode.SUCCESS.code());
        jsonMap.put(MESSAGE, CommonReturnCode.SUCCESS.name());
        jsonMap.put(SUCCESS, true);
        jsonMap.put("total", page.getTotal());
        jsonMap.put("rows", page.getData());

        return ajax(response, getJsonString(jsonMap), "text/html");
    }
    
    /**
     * <p>Description: 输出JSON成功消息，返回null</p>
     * @param response HttpServletResponse对象
     * @param message 信息字符串
     * @param map json map对象
     * @return null
     */
    public static String ajaxJsonSuccessMessage(HttpServletResponse response, String message, Map<String, Object> map) {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put(CODE, CommonReturnCode.SUCCESS.code());
        jsonMap.put(MESSAGE, message);
        jsonMap.put(SUCCESS, true);
        jsonMap.putAll(map);
        return ajax(response, getJsonString(jsonMap), "text/html");
    }
    
    /**
     * <p>Description: 输出JSON错误消息，返回null</p>
     * @param response HttpServletResponse对象
     * @param message 信息字符串
     * @return null
     */
    public static String ajaxJsonErrorMessage(HttpServletResponse response, String message) {
        Map<String, Object> jsonMap = new HashMap<String, Object>();
        jsonMap.put(CODE, CommonReturnCode.INTERNAL_SERVER_ERROR.code());
        jsonMap.put(MESSAGE, message);
        jsonMap.put(SUCCESS, false);
        return ajax(response, getJsonString(jsonMap), "text/html");
    }

    /**
     * <p>Description: 设置页面不缓存</p>
     * @param response HttpServletResponse对象
     */
    public static void setResponseNoCache(HttpServletResponse response) {
        response.setHeader("progma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Cache-Control", "no-store");
        response.setDateHeader("Expires", 0);
    }
}
